package com.originskyseed.platform.count.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.count.i18n.CountResponseMsg;
import com.originskyseed.platform.count.service.IContentCountAnchorService;
import com.originskyseed.platform.user.model.Dept;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IDeptService;
import com.originskyseed.platform.user.service.IUserInfoService;

/**
 * 总裁统计体系中主播情况数据
 *
 * @author wlj
 */
@Controller
@RequestMapping("/contentCountAnchor")
@BaseLocaleResource(localeFileName = "i18n_count")
public class ContentCountAnchorController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(ContentCountAnchorController.class);

	@DubboReference
	private IContentCountAnchorService contentCountAnchorService;

	@DubboReference
	private IDeptService deptService;

	@DubboReference
	private IUserInfoService userInfoService;

	/**
	 * 统计体系下的 主播数、礼物营收、新用户充值、优质用户、充值用户
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/countAnchorAndPlayer.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody countAnchorAndPlayer(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);

			Map<String, Object> resultMap = this.contentCountAnchorService.countContentAnchorAndPlayer(userInfo);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), resultMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 利润趋势
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/countProfitTrends.json", method = RequestMethod.POST)
	@RequiredParams(names = { "type" }) // 0日 1周 2月 3季 4年
	public BaseResponseBody countProfitTrends(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {

			// 要返回的数据
			Map<String, Object> resultMap = new HashMap<>();

			// 行和列
			List<String> columnList = new ArrayList<>();
			List<BigDecimal> rowList = new ArrayList<>();
			List<BigDecimal> row1List = new ArrayList<>();
			List<BigDecimal> row2List = new ArrayList<>();

			// 当前用户信息
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			String deptId = this.getParameterString("deptId", null);
			if (deptId == null) {
				deptId = userInfo.getDeptId();
			}

			// 类型 0日，1周，2月，3季，4年
			Integer type = this.getParameterInteger("type", null);
			if (userInfo != null) {
				// 获取下级
				Dept queryDept = this.deptService.selectByPrimaryKey(userInfo.getDeptId());
				List<Dept> deptList = this.deptService.selectByParentCode(queryDept.getParentCode());
				for (Dept dept : deptList) {
					// 所有下级部门id
					List<String> deptIds = new ArrayList<>();
					this.selectSonId(dept.getCode(), deptIds);
					if (deptIds != null && deptIds.size() > 0) {
						// 主播id
						List<String> userList = this.userInfoService.selectSalesmanIdsByDeptIds(deptIds);
						if (userList != null && userList.size() > 0) {
							// 查询数据
							Map<String, Object> map = this.contentCountAnchorService.selectBasePayAndGrossProfit(userList, type);
							rowList.add((BigDecimal) map.get("basePay"));
							row1List.add((BigDecimal) map.get("grossProfit"));
							row2List.add((BigDecimal) map.get("profit"));
							columnList.add(dept.getName());
						} else {
							continue;
						}
					}
				}
			}
			resultMap.put("columnList", columnList);
			resultMap.put("basePayList", rowList);
			resultMap.put("grossProfitList", row1List);
			resultMap.put("profitList", row2List);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), resultMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询组织架构接口
	 * 
	 * @param code
	 * @param list
	 * @throws Exception
	 */
	private void selectGroupId(String code, List<String> list) throws Exception {
		List<Dept> deptList = this.deptService.selectByParentCode(code);
		if (deptList != null) {
			for (Dept dept1 : deptList) {
				if (dept1.getType() == 6) {
					list.add(dept1.getId());
				}
				selectGroupId(dept1.getCode(), list);
			}
		}
	}

	/**
	 * 查询组织架构接口
	 * 
	 * @param code
	 * @param list
	 * @throws Exception
	 */
	private void selectSonId(String code, List<String> list) throws Exception {
		List<Dept> deptList = this.deptService.selectByParentCode(code);
		if (deptList != null) {
			for (Dept dept1 : deptList) {
				list.add(dept1.getId());
				selectSonId(dept1.getCode(), list);
			}
		}
	}

}
